package com.wwtech.epp.ui.activity.friend.presenter;

import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.AllNode;
import com.wwtech.epp.ui.activity.friend.FrameworkActivity;
import com.wwtech.epp.ui.activity.friend.contract.FrameworkContract;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.friend
 * @Description: presenter of FrameworkActivity
 * @date 2017/10/24 20:19:32
 */
public class FrameworkPresenter implements FrameworkContract.FrameworkContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final FrameworkContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private FrameworkActivity mActivity;

    @Inject
    public FrameworkPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull FrameworkContract.View view, FrameworkActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getAllNode(Map map) {
        Disposable disposable = httpAPIWrapper.getAllNode(map)
                .subscribe(new Consumer<AllNode>() {
                    @Override
                    public void accept(AllNode node) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.setAdapter(node);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        throwable.printStackTrace();
                        //mView.closeProgressDialog();
                        //ToastUtil.show(mActivity, mActivity.getString(R.string.loading_failed_1));
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }
}